require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsRuneLevelUpHandler ={}
local this = RedDotsRuneLevelUpHandler

function RedDotsRuneLevelUpHandler.init()
    return this
end

function RedDotsRuneLevelUpHandler.getFlag(...)
    local partnerId,index = ...
    if partnerId and index then
        local runeData = logicMgr.RuneManager.GetHeroRuneSrvInfo(partnerId, index)
        if runeData then
            local isMax = logicMgr.RuneManager.GetRuneLevelUpCost(runeData.itemModelId, runeData.level)
            if isMax == 0 then
                return Config.RedDotsFlagState.None
            end
            local curExp = runeData.exp
            local orginalExp = logicMgr.RuneManager.GetRuneBaseExp(runeData.itemModelId, 0)
            local need = isMax - (curExp+orginalExp-logicMgr.RuneManager.GetRuneBaseExp(runeData.itemModelId, runeData.level))
            local haveExp = 0
            local list = {}
            local it1 = ItemManager.getItemIdByModelId(200049)
            local it2 = ItemManager.getItemIdByModelId(200050)
            if it2 then
                table.insert(list, 1, clone(logicMgr.ItemManager.getItemData(it2)))
            end
            if it1 then
                table.insert(list, 1, clone(logicMgr.ItemManager.getItemData(it1)))
            end
            for k,v in pairs(list) do
                haveExp = haveExp + v.num * v.itemFunction[2]
            end
            if haveExp >= need then
                return Config.RedDotsFlagState.Needful
            else
                return Config.RedDotsFlagState.None
            end
        end
    end
    return Config.RedDotsFlagState.None
end

function RedDotsRuneLevelUpHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.RuneLevelUp);
end